package com.winxuan.channel.controller.commodity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.winxuan.channel.controller.BaseController;
import com.winxuan.channel.model.commodity.CommodityExclude;
import com.winxuan.channel.model.object.ChannelResult;
import com.winxuan.channel.service.commodity.ExcludeService;
import com.winxuan.channel.support.interceptor.PaginationAware;
import com.winxuan.util.pagination.Pagination;

@Controller
@RequestMapping(value="/commodityexclude")
public class CommodityExcludeController extends BaseController implements PaginationAware{
	/**
	 *  .排除商品列表
	 * @param distribute
	 * @param limit
	 * @param page
	 * @return
	 * @author HideHai
	 */
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public ChannelResult getLogList(@RequestParam("distribute")Integer distribute,
			@RequestParam("commodity")Integer commodity,
			@RequestParam("limit")Integer limit,
			@RequestParam("page")Integer page){
		ChannelResult channelResult = new ChannelResult();
		pagination.setPageSize(limit);
		pagination.setCurrentPage(page);
		List<CommodityExclude> commodityLogs = excludeService.getExcludeCommodityByDistribute(distribute,commodity,pagination);
		if(commodityLogs != null){
			channelResult.setSuccess(true);
			channelResult.setResult(commodityLogs);
			channelResult.setTotalCount(pagination.getCount());
			return channelResult;
		}
		channelResult.setSuccess(false);
		channelResult.setResult(null);
		return channelResult;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ChannelResult addExcludeCommodity(@RequestParam("commodity")Integer commodity,
			@RequestParam("distribute")Integer distribute,@RequestParam("enabledate")String enabledate,
			@RequestParam("disabledate")String disabledate,@RequestParam("syncStock")boolean syncStock,
			@RequestParam("remark")String remark) throws ParseException{
		ChannelResult channelResult = new ChannelResult();
		CommodityExclude commodityExclude = new CommodityExclude();
		commodityExclude.setCommodity(commodity);
		commodityExclude.setDistribute(distribute);
		commodityExclude.setCreatetime(new Date());
		commodityExclude.setEnabledate(SimpleDateFormat.getDateTimeInstance().parse(enabledate));
		commodityExclude.setDisabledate(SimpleDateFormat.getDateTimeInstance().parse(disabledate));
		commodityExclude.setSyncStock(syncStock);
		commodityExclude.setRemark(remark);
		excludeService.addExcludeCommodity(commodityExclude);
		channelResult.setSuccess(true);
		return channelResult;
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public ChannelResult editExcludeCommodity(Integer id,Integer commodity,
			Integer distribute,String enabledate,String disabledate,boolean syncStock,
			String remark) throws ParseException{
		ChannelResult channelResult = new ChannelResult();
		CommodityExclude commodityExclude = excludeService.getExcludeCommodity(id);
		commodityExclude.setCommodity(commodity);
		commodityExclude.setDistribute(distribute);
		commodityExclude.setCreatetime(new Date());
		commodityExclude.setEnabledate(SimpleDateFormat.getDateTimeInstance().parse(enabledate));
		commodityExclude.setDisabledate(SimpleDateFormat.getDateTimeInstance().parse(disabledate));
		commodityExclude.setSyncStock(syncStock);
		commodityExclude.setRemark(remark);
		excludeService.updateExcludeCommodity(commodityExclude);
		channelResult.setSuccess(true);
		return channelResult;
	}
	
	@RequestMapping(value="/del",method=RequestMethod.POST)
	public ChannelResult deleteExcludeCommodity(String ids){
		ChannelResult channelResult = new ChannelResult();
		if(ids != null & ids.length()>0){
			String[] id = ids.split(",");
			for(String i : id){
				CommodityExclude commodityExclude = excludeService.getExcludeCommodity(Integer.valueOf(i));
				excludeService.delelteExcludeCommodity(commodityExclude);	
			}
		}
		channelResult.setSuccess(true);
		return channelResult;
	}
	
	@RequestMapping(value="/id/{id}/sync",method=RequestMethod.POST)
	public ChannelResult syncStockExcludeCommodity(String ids,boolean syncStock){
		ChannelResult channelResult = new ChannelResult();
		if(ids != null & ids.length()>0){
			String[] id = ids.split(",");
			for(String i : id){
				CommodityExclude commodityExclude = excludeService.getExcludeCommodity(Integer.valueOf(i));
				commodityExclude.setSyncStock(syncStock);
				excludeService.updateExcludeCommodity(commodityExclude);	
			}
		}
		channelResult.setSuccess(true);
		return channelResult;
	}
	
	@Autowired
	private ExcludeService excludeService;

	@Override
	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
}
